package cn.xqj.org.controller;

import cn.xqj.basic.util.AjaxResult;
import cn.xqj.basic.util.PageList;
import cn.xqj.org.domain.Employee;
import cn.xqj.org.query.EmployeeQuery;
import cn.xqj.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 员工接口 员工crud
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 员工接口：查询单个员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable Long id) {
        return employeeService.findOne(id);
    }


    /**
     * 员工接口：查询所有员工信息
     * @return
     */
    @GetMapping
    public List<Employee> findAll() {
        return employeeService.findAll();
    }

    /**
     * 员工接口：员工列表分页查询和高级查询
     * @param employeeQuery
     * @return
     */
    @PostMapping // 分页查询高级查询需要在请求体中传json对象参数  所以用post请求
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.pageQuery(employeeQuery);
    }

    /**
     * 员工接口：添加和修改接口  （添加没有id  修改有id）
     * @param employee
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }
    }


    /**
     * 员工接口：删除单个员工信息
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id) {
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }
    }


    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long [] ids) {
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }

    }


    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId) {
        return employeeService.list(shopId);
    }
}
